package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

/**
 * 前台系统的在线下单系统管理
 * 
 * @author 艾欣
 *
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
	private String sendAreaInfo; // 获取寄件人地址
	private String recAreaInfo; // 获取收件人地址

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	/**
	 * 添加订单
	 */
	@Action(value = "order_add", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String add() {
		// 把地址封装起来
		Area sendArea = new Area();
		String[] send = sendAreaInfo.split("/");
		sendArea.setProvince(send[0]);
		sendArea.setCity(send[1]);
		sendArea.setDistrict(send[2]);
		// 封装收件人地址
		Area recArea = new Area();
		// 分割字符串
		String[] rec = recAreaInfo.split("/");
		recArea.setProvince(rec[0]);
		recArea.setCity(rec[1]);
		recArea.setDistrict(rec[2]);
		// 把两个地址封装到订单中
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		// 把订单关联客户
		// 从前台获取客户信息
		Customer customer = (Customer) ServletActionContext.getRequest()
				.getSession().getAttribute("customer");
		// 把客户信息保存到订单中
		model.setCustomer_id(customer.getId());
		System.out.println(model);
		// 把订单保存到数据库中
		
		
		WebClient
				.create(Constants.BOS_MANAGEMENT_URL
						+ "/services/orderService/order")
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;

	}
}
